#include "cpu.h"
#include "includes.h"


OS_TCB tcbtmp;
OS_STK stk1[5];
unsigned int tickcnt;
void Led1Test(void *pdata);

void delay(unsigned int t)
{
	unsigned i,j;
	for(i = 0; i < t; i++)
		{
		for(j= 0; j < 5000; j++);
		}
}


void Led1Test(void *pdata)
{
	
	unsigned int i=0;
	
	GPBCON &= ~(0XF << 10);
	GPBCON |=  (0X5 << 10);
	
	while(1)
		{
		GPBDAT |= (1 << 5);
		delay(50);
		GPBDAT &= ~(1 << 5);
		delay(50);
		i++;
		if(i == 4)
		{
			OSTimeDly(2);
			i = 0;
			}
		}
}



void MyTask(void *pdata)
{
	unsigned int i;
	i = 0;
	tcbtmp.OSTCBStkPtr = stk1;
	Timer4Init();
	GPBCON &= ~(0XF << 10);
	GPBCON |=  (0X5 << 10);

	GPBCON &= ~(0XF << 10);
	GPBCON |=  (0X5 << 10);
	tickcnt =0;
	//OSTaskCreate(Led1Test,(void *)0, &mytaskstk1[99], 4);

	while(1)
		{
		GPBDAT |= (1 << 6);
		delay(50);
		GPBDAT &= ~(1 << 6);
			delay(50);
		i++;
		if(i == 4)
			{
			OSTimeDly(2);
			
			i=0;
			}
		
			
			
			
		}
}


void Timer4Init(void)
{
	INTMSK &= ~(1 << 14);	//Unmask timer4 interrupt
	INTMOD &= ~(1 << 14);	//timer4 irq interrupt mode
	TCFG0 &= ~(0xff << 8);
	TCFG0 |= (0Xff << 8);
	TCFG1 &= ~(0XF << 16);
	TCFG1 |= (3 << 16);
	TCNTB4 = 0XFFFf;
	TCON |=(1 << 21);
	TCON &= ~(1 <<21);
	TCON |= (1 << 22)|(1 << 20);
	
}
